﻿using System;
using SharpKit.JavaScript;

namespace SharpKit.Onx.Playground.Recipes
{
    [JsType(JsMode.Global, Filename = "../../res/TweetOnX.js")]
    public class TweetOnXSample : OnxContextBase
    {
        /// <summary>
        /// Simply change your screenName, onxToken and Tweet from the code below. And insert it in whatever trigger that you'd like to fire the tweet.
        /// </summary>
        [JsMethod(GlobalCode = true)]
        static void Configure()
        {
            // Initializing variables
            var twitter = new { screenName = "tweetonx", onxToken = "8f8xv9fw" };

            var tweet = encodeURI("Testing Tweet on{X} :)");

            dynamic headers = new object();
            headers["Content-Type"] = "application/xml";

            // Sends tweet using TweetOn{X}
            Device.Ajax(new AjaxArguments
            {
                Url = "http://tweetonx.com/api/tweet/" + twitter.screenName + "/" + twitter.onxToken + "/?t=" + tweet,
                Type = AjaxType.Get,
                Headers = headers
            },
            (body, textStatus, response) =>
            {
                if (!(body.As<bool>()))
                {
                    dynamic error = new object();
                    error.message = "invalid body format";
                    error.content = body;
                    Console.Error("error: ", error);
                }
                Console.Info("Status: " + body);
            },
            (textStatus, response) =>
            {
                dynamic error = new object();
                error.message = textStatus;
                error.statusCode = response.Status;
                Console.Error("error: ", error);
            }
            );
        }
    }
}